package com.project.util;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
 * 持久层工厂类
 */
public class DaoFactory {
    private static Properties pro = new Properties();
    static {
        try {
            pro.load((new FileReader("dao.txt")));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static  Object getDao(String daoName){
        //根据键得到值
        String str = pro.getProperty(daoName);
        try {
            //加载类，得到类模板
            Class c = Class.forName(str);

            //调用无参构造方法，得到该类的对象
            return c.getConstructor().newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
